package common.utils.annotations;


import common.utils.DesensitizationUtils;
import common.utils.enums.SensitiveTypeEnum;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.format.Formatter;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Locale;

@Component
@Data
public class DesensitizationFormatter implements Formatter<String> {

    private SensitiveTypeEnum typeEnum;

    @Override
    public String print(String str, Locale locale) {
        return str;
    }

    @Override
    public String parse(String valueStr, Locale locale) throws ParseException {
        System.out.println("valueStr = " + valueStr);

        if (StringUtils.isNotBlank(valueStr)) {
            switch (typeEnum) {
                case CHINESE_NAME:
                    valueStr = DesensitizationUtils.chineseName(valueStr);
                    break;
                case ID_CARD:
                    valueStr = DesensitizationUtils.idCardNum(valueStr);
                    break;
                case FIXED_PHONE:
                    valueStr = DesensitizationUtils.fixedPhone(valueStr);
                    break;
                case MOBILE_PHONE:
                    valueStr = DesensitizationUtils.mobilePhone(valueStr);
                    break;
                case ADDRESS:
                    valueStr = DesensitizationUtils.address(valueStr, 8);
                    break;
                case EMAIL:
                    valueStr = DesensitizationUtils.email(valueStr);
                    break;
                case BANK_CARD:
                    valueStr = DesensitizationUtils.bankCard(valueStr);
                    break;
                case PASSWORD:
                    valueStr = DesensitizationUtils.password(valueStr);
                    break;
                case CARNUMBER:
                    valueStr = DesensitizationUtils.carNumber(valueStr);
                    break;
                case RICH_TEXT_CONTENT:
                    valueStr = DesensitizationUtils.richTextContent(valueStr);
                    break;
                case UUIDS:
                    valueStr = DesensitizationUtils.uuids(valueStr);
                    break;
                case STRINGDEFAULTVALUE:
                    valueStr = DesensitizationUtils.stringDefaultValue(valueStr);
                    break;
                case SORT_TYPE:
                    valueStr = DesensitizationUtils.sortType(valueStr);
                    break;
                default:
            }
        }
        return valueStr;
    }

}
